require "common"
require "uiWin"
require "st7735"
module(..., package.seeall)
-- LCD分辨率的宽度和高度(单位是像素)、BPP位深度
WIDTH, HEIGHT, BPP = disp.getlcdinfo()
-- 1个ASCII字符宽度为8像素，高度为16像素；汉字宽度和高度都为16像素
CHAR_WIDTH = 8
LCD_ACTIVE, LCD_CONTENT = 1, {} -- 当前显示屏幕索引
LCD_DATA = {'USER USE', 'USER USE', 'USERDATA:NOREADY'}
LINE_BAR = 160
DTU_ALARM = "NOSET"
DTU_ID = "NOSET"
USR_STATUS = ''
local status = "GSM注册中"
DTU, UDTU = {}, {}

function setDtu(dtu, udtu)
    DTU = dtu
    UDTU = udtu
end
--[[
函数名：getxpos
功能  ：计算字符串居中显示的X坐标
参数  ：
str：string类型，要显示的字符串
返回值：X坐标
]]
function getxpos(str) return (WIDTH - string.len(str) * CHAR_WIDTH) / 2 end

function setcolor(color) if BPP ~= 1 then return disp.setcolor(color) end end
LCD_CONTENT[1] = function()
    -- 在线状态
    if create.getDatalink() then
        status = "在线"
        links = create.getDatalinks()
        for i, v in pairs(links) do status = status .. (v and i or '') end
    else
        status = net.getState()
    end

    -- 电压信号强度
    LCD_DATA[1] = string.format('VOL:%.1fV CSQ:%d:%dG', power.getVbat(), (net.getRssi() or 0),(net.getNetMode() or 0))
    -- sim卡号码
    if sim.getIccid() and #sim.getIccid() > 6 then
        if power.getFlag() and tonumber(power.getFlag())>0 then
            DTU_ID = string.sub(misc.getImei(), -6)
        else
            DTU_ID = string.sub(sim.getIccid(), -6)
        end
        LCD_DATA[2] = 'ID:' .. DTU_ID
    else
        LCD_DATA[2] = "SIM:NOSIM"
    end
    LCD_DATA[2] = LCD_DATA[2] .. " ALM:" .. DTU_ALARM
    -- 清空LCD显示缓冲区
    disp.clear()
    local oldColor = setcolor(0xF800) -- 红色
    if (LINE_BAR > 0) then
        LINE_BAR = LINE_BAR - 1
        disp.drawrect(0, 124, LINE_BAR, 127, 0xF800)
    end
    disp.puttext(PROJECT .. " V" .. VERSION, getxpos(PROJECT .. " V" .. VERSION), 0)
    -- local tm = misc.getClock()
    local tm = pcf8563.read_time()
    if not tm or not tm.year then tm = misc.getClock() end
    local datestr = string.format("%04d-%02d-%02d ", tm.year, tm.month, tm.day)
    local timestr = string.format("%02d:%02d:%02d", tm.hour, tm.min, tm.sec)
    -- 显示日期
    setcolor(0x001F) -- 蓝色
    disp.puttext(datestr .. timestr, getxpos(datestr .. timestr), 20)
    setcolor(0x07E0) -- 绿,显示网络情况
    if (LINE_BAR > 0) then status = status .. string.format(" %03dOFF", LINE_BAR) end
    if (USR_STATUS and USR_STATUS ~= '') then status = status .. USR_STATUS end
    disp.puttext(common.utf8ToGb2312(status), getxpos(common.utf8ToGb2312(status)), 40)
    setcolor(0x001F) -- 蓝色
    for i, v in pairs(LCD_DATA) do
        -- log.warn("lcd",lcd_data[i] )
        if (v and v ~= "") then disp.puttext(common.utf8ToGb2312(v), 2, i * 20 + 40) end
    end
    -- 刷新LCD显示缓冲区到LCD屏幕上
    disp.update()
    setcolor(oldColor)
end

LCD_CONTENT[2] = function()
    qwidth, qdata = qrencode.encode("http://gprs.yuanfen360.cn/session/cfg/" .. DTU_ID)
    disp.clear()
    local oldColor = setcolor(0x001F)
    local title = common.utf8ToGb2312("扫码进入配置页面")
    disp.puttext(title, getxpos(title), 3)
    disp.putqrcode(qdata, qwidth, 100, 35, 23)
    local bottom = "ID:"..DTU_ID
    disp.puttext(bottom,  getxpos(bottom), 111)
    disp.update()
    setcolor(oldColor)
end
LCD_DATA3 = {}
LCD_CONTENT[3] = function()
    disp.clear()
    local oldColor = setcolor(0x001F)
    disp.puttext(common.utf8ToGb2312("设备ID:" .. (UDTU.dsn or "NOSET")), 1, 2)
    LCD_DATA3[1] = "启动时长:" .. (UDTU.ontime or "NOSET") .. "分钟"
    LCD_DATA3[2] = "上报时间:" .. (UDTU.alarm[1] or 0) .. ":" .. (UDTU.alarm[2] or 0) .. "分钟"
    LCD_DATA3[3] = "DTYPE:" .. (UDTU.dtype or 0) .. " NTYPE:" .. (UDTU.ntype or 0)
    LCD_DATA3[4] = "IP:" .. (DTU.conf[3] and DTU.conf[3][4] or "NOSET")
    LCD_DATA3[5] = "PORT:" .. (DTU.conf[3] and DTU.conf[3][5] or "NOSET").. " UART:"..(DTU.conf[3] and DTU.conf[3][6] or "NOSET")
    for i, v in pairs(LCD_DATA3) do if v then disp.puttext(common.utf8ToGb2312(v), 1, i * 20 + 2) end end
    disp.update()
    setcolor(oldColor)
end
LCD_DATA4 = {}
LCD_CONTENT[4] = function()
    disp.clear()
    local oldColor = setcolor(0x001F)
    disp.puttext(common.utf8ToGb2312("U-RS485:" .. (DTU.uconf[3][1] or "NOSET")), 1, 2)
    LCD_DATA4[1] = "波 特 率:" .. (DTU.uconf[3][2] or "NOSET")
    LCD_DATA4[2] = "数据长度:" .. (DTU.uconf[3][3] or "NOSET")
    LCD_DATA4[3] = "校 验 位:" --校验位,0是uart.PAR_EVEN,1是uart.PAR_ODD,2是uart.PAR_NONE
    if  tonumber(DTU.uconf[3][4])==0 then LCD_DATA4[3]= LCD_DATA4[3].."偶校验"
    elseif tonumber(DTU.uconf[3][4])==1 then LCD_DATA4[3]= LCD_DATA4[3].."奇校验"
    elseif tonumber(DTU.uconf[3][4])==2 then  LCD_DATA4[3]= LCD_DATA4[3].."NONE"
    else  LCD_DATA4[3]= LCD_DATA4[3].."NOSET" end
    LCD_DATA4[4] = "停 止 位:" .. (DTU.uconf[3][5]==0 and 1 or (DTU.uconf[3][5]==2 and 2 or "NOSET")) --停止位,0是1个停止位,2是2个停止位
    LCD_DATA4[5] = "RS485DIR:" ..  (DTU.uconf[3][6]  or "NOSET")
    for i, v in pairs(LCD_DATA4) do if v then disp.puttext(common.utf8ToGb2312(v), 1, i * 20 + 2) end end
    disp.update()
    setcolor(oldColor)
end
LCD_DATA5 = {}
LCD_CONTENT[5] = function()
    disp.clear()
    local oldColor = setcolor(0x001F)
    disp.puttext(common.utf8ToGb2312("输出状态:" .. power.GET_QC(1)..power.GET_QC(2)..power.GET_QC(3)..power.GET_QC(4)..power.GET_QC(5)..power.GET_QC(6)..power.GET_QC(7)..power.GET_QC(8)), 1, 2)
    LCD_DATA5[1] = "输入状态:" .. power.GET_DI(1)..power.GET_DI(2)..power.GET_DI(3)..power.GET_DI(4)..power.GET_DI(5)..power.GET_DI(6)..power.GET_DI(7)..power.GET_DI(8)
    for i, v in pairs(LCD_DATA5) do if v then disp.puttext(common.utf8ToGb2312(v), 1, i * 20 + 2) end end
    disp.update()
    setcolor(oldColor)
end
local function open() if (LCD_CONTENT[LCD_ACTIVE]) then LCD_CONTENT[LCD_ACTIVE]() end end
local function lcd_switch(key, m)
    if key>0 then return end
    LCD_ACTIVE = LCD_ACTIVE + 1
    if LCD_ACTIVE > #LCD_CONTENT then LCD_ACTIVE = 1 end
end
sys.subscribe("DTU_KEY_PRESS_0", lcd_switch)
sys.timerLoopStart(open, 1000)
